package com.xjm.ips.controller;

import com.xjm.ips.entity.AjaxJson;
import com.xjm.ips.entity.Department;
import com.xjm.ips.service.RedisService;
import com.xjm.ips.service.ViewService;
import com.xjm.ips.utils.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    RedisService redisService;
    @Autowired
    ViewService viewService;
    @GetMapping("/login")
    public AjaxJson login(){
        return AjaxJson.error("尚未登陆，请登陆");
    }

    @GetMapping("/redis/setAndGet")
    public String setAndGetValue(String name,String value){
        redisService.set(name,value);
        return redisService.get(name).toString();
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpSession httpSession, HttpServletResponse httpServletResponse) throws IOException {
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        String text = verifyCode.getText();
        httpSession.setAttribute("verifyCode",text);
        VerifyCode.output(image,httpServletResponse.getOutputStream());
    }

    @GetMapping("/getEveryDeparmentNumbers")
    public List<Department> getEveryDeparmentNumbers(){
        return viewService.getEveryDeparmentNumbers();
    }
}
